<template>
    <div class="list-container">
        <Search @clickMR="checkEdit" />
        <MRTable ref="MRTable" v-model="infoData" @saveFinish="cancelSave" />
        <el-dialog
                title="提示"
                :visible.sync="centerDialogVisible"
                width="30%"
                @close="centerDialogVisible = false"
                center>
            <span>是否进行保存</span>
            <span slot="footer" class="dialog-footer">
            <el-button @click="cancelSave">取消</el-button>
            <el-button @click="zancun">暂存</el-button>
            <el-button type="primary" @click="submitThis">提交</el-button>
          </span>
        </el-dialog>
    </div>
</template>

<script>
    import MRTable from '@/views/list/components/MRTable'
    import Search from '@/views/list/components/search'
    import {dateNumFormat} from '@/utils/filters'
    import {searchMR} from '@/api/search'

    export default {
        name: 'index',
        data () {
            return {
                // 所有数据
                infoData: {
                    noneCodingError: [],
                    noneCodingWaring: [],
                    codingError: [],
                    codingWaring: [],
                    allDeduction: [],
                    DRGsData: {},
                    resExamine: []
                },
                // 弹框显示
                centerDialogVisible: false,
                // 点击的id
                nowId: ''
            }
        },
        created () {
        },
        mounted () {
        },
        methods: {
            /**
             * 提交当前内容并关闭
             */
            submitThis () {
                localStorage.removeItem(this.infoData.ID)
                this.$refs.MRTable.QCEdit('submit', 'close')
            },
            /**
             * 确认是否已修改
             * @param id 病案id
             * @param bool 是否相同
             */
            checkEdit (id, bool) {
                if (this.infoData.ID) {
                    this.nowId = id
                    let editTableForm = this.$refs.MRTable.getVal('editTableForm')
                    let editData = {
                        ...editTableForm,
                        noneCodingError: [],
                        noneCodingWaring: [],
                        codingError: [],
                        codingWaring: [],
                        allDeduction: [],
                        DRGsData: {},
                        resExamine: []
                    }
                    let infoData = {
                        ...this.infoData,
                        noneCodingError: [],
                        noneCodingWaring: [],
                        codingError: [],
                        codingWaring: [],
                        allDeduction: [],
                        DRGsData: {},
                        resExamine: []
                    }
                    if (JSON.stringify(editData) === JSON.stringify(infoData)) {
                        this.checkLocal(id)
                    } else {
                        this.centerDialogVisible = true
                    }
                } else {
                    this.checkLocal(id)
                }
            },
            /**
             * 提交保存后获取下一步
             */
            saveFinish () {
                this.centerDialogVisible = false
                this.checkLocal(this.nowId)
            },
            /**
             * 取消保存
             */
            cancelSave () {
                this.centerDialogVisible = false
                this.checkLocal(this.nowId)
            },
            /**
             * 暂存
             */
            zancun () {
                let editTableForm = this.$refs.MRTable.getVal('editTableForm')
                // 字段要大些，因为转过一次
                let data = {
                    data: editTableForm,
                    dataInfo: {
                        BAH: editTableForm.BAH,
                        ZYCS: editTableForm.ZYCS,
                        XM: editTableForm.XM,
                        time: this.parseTime(new Date().getTime())
                    }
                }
                localStorage.setItem(editTableForm.ID, JSON.stringify(data))
                this.centerDialogVisible = false
                this.$message.success('暂存成功')
                setTimeout(() => {
                    this.checkLocal(this.nowId)
                }, 300)
            },
            /**
             * 确认是否有暂存
             * @param id 病案id
             */
            checkLocal (id) {
                if (localStorage.getItem(id)) {
                    let dataInfo = JSON.parse(localStorage.getItem(id)).dataInfo
                    let text = `该患者有暂存信息是否恢复？<br/>暂存时间：${dataInfo.time}<br/>病 案 号：${dataInfo.BAH}<br/>患者姓名：${dataInfo.XM}<br/>住院次数：${dataInfo.ZYCS}`
                    let title = '恢复暂存'
                    this.$confirm(text, title, {
                        dangerouslyUseHTMLString: true,
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                        center: false
                    }).then(() => {
                        let data = JSON.parse(localStorage.getItem(id)).data
                        this.infoData = {
                            ...data,
                            noneCodingError: [],
                            noneCodingWaring: [],
                            codingError: [],
                            codingWaring: [],
                            allDeduction: [],
                            DRGsData: {},
                            resExamine: []
                        }
                    }).catch(() => {
                        localStorage.removeItem(id)
                        this.getMR(id)
                    })
                } else {
                    this.getMR(id)
                }
            },
            /**
             * 获取数据
             * @param id 病案id
             */
            getMR (id) {
                localStorage.removeItem(id)
                let params = {
                    id
                }
                searchMR(params).then(res => {
                    let obj = {}
                    Object.keys(res).forEach(key => {
                        obj[key.toUpperCase()] = res[key]
                    })
                    this.infoData = {
                        ...this.infoData,
                        ...obj,
                        noneCodingError: [],
                        noneCodingWaring: [],
                        codingError: [],
                        codingWaring: [],
                        allDeduction: [],
                        DRGsData: {},
                        resExamine: []
                    }
                    this.infoData.ZKKB = this.infoData.ZKKB ? this.infoData.ZKKB.split(',') : this.infoData.ZKKB
                    this.infoData.CSRQ = dateNumFormat(this.infoData.CSRQ)
                    this.infoData.RYSJ = dateNumFormat(this.infoData.RYSJ)
                    this.infoData.CYSJ = dateNumFormat(this.infoData.CYSJ)
                    this.infoData.ZKRQ = dateNumFormat(this.infoData.ZKRQ)
                    this.infoData.SSJCZRQ1 = dateNumFormat(this.infoData.SSJCZRQ1)
                    this.infoData.SSJCZRQ2 = dateNumFormat(this.infoData.SSJCZRQ2)
                    this.infoData.SSJCZRQ3 = dateNumFormat(this.infoData.SSJCZRQ3)
                    this.infoData.SSJCZRQ4 = dateNumFormat(this.infoData.SSJCZRQ4)
                    this.infoData.SSJCZRQ5 = dateNumFormat(this.infoData.SSJCZRQ5)
                    this.infoData.SSJCZRQ6 = dateNumFormat(this.infoData.SSJCZRQ6)
                    this.infoData.SSJCZRQ7 = dateNumFormat(this.infoData.SSJCZRQ7)
                })
            }
        },
        components: {
            MRTable,
            Search
        }
    }
</script>

<style scoped lang="scss">
    .list-container {
        display: flex;
        justify-content: center;
        align-items: flex-start;
        padding: 6px 0;
    }
</style>
